import { Injectable } from '@angular/core';
import {Actions, ofType, Effect} from '@ngrx/effects';
import { Observable, of , pipe} from 'rxjs';
import * as actions from '../actions/quote.action';
import {LoadAction} from '../actions/quote.action';
import { Action } from '@ngrx/store';
import { QuoteService } from '../services/quote.service';
import { switchMap, map, catchError } from 'rxjs/operators';
@Injectable()
export class QuoteEffects {
    @Effect()
    quote$:Observable<Action> = this.actions$.pipe(
        ofType(actions.ActionTypes.LOAD),
        map((a: LoadAction) => a.payload),
        switchMap( _ => this.quoteService.getQuote().pipe(
            map(q => new actions.LoadSuccessAction(q)),
            catchError(err => of(new actions.LoadFailAction(err)))
        ))
    );
    constructor(
        public actions$:Actions,
        public quoteService:QuoteService
      ) {}
}
